package cn.blunce.utils.XmlParse;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;


/**
 * Created by Blunce on 2015/11/11 0011.
 */
public class XstreamParse {
    /**
     * 简单解析
     *
     * @param person
     * @return
     */
    public static String toXml(Person person) {
        return new XStream().toXML(person);
    }

    /**
     * 简单解析
     *
     * @param xml
     * @return
     */
    public static Person toPerson(String xml) {
        return (Person) new XStream().fromXML(xml);
    }

    /**
     * 添加别名解析
     *
     * @param person
     * @return
     */
    public static String toXmlPro(Person person) {
        XStream xStream = new XStream();
        xStream.alias("person", Person.class);
        return xStream.toXML(person);
    }

    /**
     * 添加别名解析
     *
     * @param xml
     * @return
     */
    public static Person toPersonPro(String xml) {
        XStream xStream = new XStream();
        xStream.alias("person", Person.class);
        return (Person) xStream.fromXML(xml);
    }


    /**
     * json解析
     *
     * @param person
     * @return
     */
    public static String PersonToJson(Person person) {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("product", Person.class);
        return xstream.toXML(person);
    }

    /**
     * json解析
     *
     * @param xml
     * @return
     */
    public static Person JsonToPerson(String xml) {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.alias("product", Person.class);
        return (Person) xstream.fromXML(xml);
    }
}
